//
//  DB.m
//  UI_DataBase
//
//  Created by LiuFei on 14/11/10.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "DB.h"
#import <sqlite3.h>

static sqlite3 *dbPoint = nil;

@implementation DB

+ (sqlite3 *)shareDataBase
{
    if (dbPoint) {
        return dbPoint;
    }
    // 1 将bundle文件夹中数据库拷贝到documents
    // 获取documents的路径
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *toPath = [arr lastObject];
    // documents初始化时：文件不存在
    toPath = [NSString stringWithFormat:@"%@/Trip.db", toPath];
    NSLog(@"toPath = %@", toPath);
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL judge = [manager fileExistsAtPath:toPath];
    if (!judge) {
        // 文件不存在，拷贝文件
        NSString *srcPath = [[NSBundle mainBundle] pathForResource:@"Trip" ofType:@"rdb"];
        // 拷贝文件
        [manager copyItemAtPath:srcPath toPath:toPath error:nil];
        
    }
    // 2 获得数据库指针(添加库文件)
    // 将NSString转换为Char:[toPath UTF8String]
    // 通过toPath文件找到数据库，打开数据库并给dbPoint赋值
    sqlite3_open([toPath UTF8String], &dbPoint);
    return dbPoint;
}

































@end
